/** 
 * File-Name:AppAction.java
 *
 * Created on 2011-6-3 下午04:43:58
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package xiaoyou.web;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.log4j.Logger;

import xiaoyou.entity.Article;
import xiaoyou.entity.Fund;
import xiaoyou.entity.Project;
import xiaoyou.entity.User;
import xiaoyou.neoutil.CommonAction;
import xiaoyou.service.ArticleRepository;
import xiaoyou.service.FundRepository;
import xiaoyou.service.ProjectRepository;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-3 下午04:43:58
 */
public class AppAction extends CommonAction {
	private static Logger log = Logger.getLogger(AppAction.class);
	private List<Fund> latestFunds;
	private List<Project> latestProjects;
	private List<Article> latestNotes;
	private List<Article> latestNotices;
	private List<Article> latestNews;
	private List<Article> latestChizi;
	private List<Article> latestFundUsages;

	private String htmlData;

	public String toHome() {
		try {
			FundRepository fundRepository = new FundRepository();
			latestFunds = fundRepository.getLatestFunds(6);

			ProjectRepository projectRepository = new ProjectRepository();
			latestProjects = projectRepository.getLatestProjects(6);

			/**
			 * 首页最新“通知公告”
			 */
			ArticleRepository articleRepository = new ArticleRepository();
			latestNotices = articleRepository.getLatestArticlesByType(
					Article.NOTICE, 7);

			/**
			 * 首页“工作动态”
			 */
			latestNews = articleRepository.getLatestArticlesByType(
					Article.NEWS, 7);
			/***
			 * 显示图片
			 */
			latestNotes = articleRepository.getLatestArticlesByType(
					Article.NEWS, 5);

			latestFundUsages = articleRepository.getLatestArticlesByType(
					Article.FUND_USAGE, 6);

			latestChizi = articleRepository.getLatestArticlesByType(
					Article.DONATOR_COMMENT, 1000);

			try {
				URL url = new URL("http://alumni.ustb.edu.cn/index.do");
				HttpURLConnection url_con = (HttpURLConnection) url
						.openConnection();
				url_con.setDoOutput(true);
				url_con.setRequestMethod("GET");
				url_con.getOutputStream().flush();
				url_con.getOutputStream().close();
				StringBuffer temp = new StringBuffer();
				InputStream in = url_con.getInputStream();
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						in, Charset.forName("UTF-8")));
				String aa = "";
				while (aa != null) {
					temp.append(aa);
					aa = rd.readLine();
				}
				String str = temp.toString();
				int dontIndex = str.indexOf("<div id=\"donate\">");
				String subStr = str.substring(dontIndex);
				int endIndex = subStr.indexOf("</div>");

				htmlData = subStr.substring("<div id=\"donate\">".length(),
						endIndex);
			} catch (Exception ex) {
				log.error(ex);
				htmlData = "加载数据出错,请稍后再试.";
			}

			log.info(htmlData);

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String toManageHome() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public List<Fund> getLatestFunds() {
		return latestFunds;
	}

	public void setLatestFunds(List<Fund> latestFunds) {
		this.latestFunds = latestFunds;
	}

	public List<Project> getLatestProjects() {
		return latestProjects;
	}

	public void setLatestProjects(List<Project> latestProjects) {
		this.latestProjects = latestProjects;
	}

	public List<Article> getLatestNotes() {
		return latestNotes;
	}

	public void setLatestNotes(List<Article> latestNotes) {
		this.latestNotes = latestNotes;
	}

	public List<Article> getLatestNotices() {
		return latestNotices;
	}

	public void setLatestNotices(List<Article> latestNotices) {
		this.latestNotices = latestNotices;
	}

	public List<Article> getLatestNews() {
		return latestNews;
	}

	public void setLatestNews(List<Article> latestNews) {
		this.latestNews = latestNews;
	}

	public List<Article> getLatestChizi() {
		return latestChizi;
	}

	public void setLatestChizi(List<Article> latestChizi) {
		this.latestChizi = latestChizi;
	}

	public List<Article> getLatestFundUsages() {
		return latestFundUsages;
	}

	public void setLatestFundUsages(List<Article> latestFundUsages) {
		this.latestFundUsages = latestFundUsages;
	}

	public void setHtmlData(String htmlData) {
		this.htmlData = htmlData;
	}

	public String getHtmlData() {
		return htmlData;
	}
}
